package com.yonyou.pmclouds.plan.planindex.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.measureindex.entity.MeasureIndexHistoryVO;
import com.yonyou.pmclouds.measureindex.entity.MeasureIndexVO;
import com.yonyou.pmclouds.measureindex.service.rmiitf.MeasureIndexQuery;
import com.yonyou.pmclouds.plan.planindex.entity.PlanIndexVO;
import com.yonyou.pmclouds.plan.planindex.mapper.PlanIndexMapper;
import com.yonyou.pmclouds.plan.planindex.rmiitf.IPlanIndexQuery;
import com.yonyou.pmclouds.plan.rmiitf.IScheduleItemQueryService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * @author th
 * @date 2019/11/14 10:09
 */
@Service(interfaceClass = IPlanIndexQuery.class, version = ApplicationConsts.APPLICATION_VERSION)
public class PlanIndexQueryImpl implements IPlanIndexQuery {
    @Autowired
    private PlanIndexMapper mapper;
    @Autowired
    private MeasureIndexQuery measureIndexQuery;
    @Autowired
    private IScheduleItemQueryService itemQueryService;

    @Override
    public List<PlanIndexVO> queryByPks(List<String> pkIndexes) {
        if (CollectionUtils.isEmpty(pkIndexes))
            return new ArrayList<>();

        return mapper.queryByPks(pkIndexes);
    }

    @Override
    public List<PlanIndexVO> queryIndexOfPlan(String pkPlan) {
        if (StringUtils.isEmpty(pkPlan))
            return new ArrayList<>(0);

        // 查询计划指标
        List<PlanIndexVO> indexVOs = mapper.queryIndexOfPlan(pkPlan);
        if (CollectionUtils.isEmpty(indexVOs))
            return new ArrayList<>(0);

        Set<String> measureIndexSet = new HashSet<>();
        for (PlanIndexVO indexVO : indexVOs) {
            measureIndexSet.add(indexVO.getPkMeasureIndex());
        }

        // 查询指标档案
        Map<String, MeasureIndexVO> measureIndexVOMap = measureIndexQuery.queryByPks(measureIndexSet.toArray(new String[0]));
        MeasureIndexVO measureIndexVO;
        for (PlanIndexVO indexVO : indexVOs) {
            measureIndexVO = measureIndexVOMap.get(indexVO.getPkMeasureIndex());
            if (measureIndexVO == null)
                continue;
            indexVO.setCode(measureIndexVO.getCode());
            indexVO.setName(measureIndexVO.getName());
            indexVO.setUnit(measureIndexVO.getUnit());
            indexVO.setMemo(measureIndexVO.getMemo());
        }

        return indexVOs;
    }

    @Override
    public Map<String, List<PlanIndexVO>> queryIndexOfPlans(String[] pkPlans) {
        Map<String, List<PlanIndexVO>> result = new HashMap<>();
        if (ArrayUtils.isEmpty(pkPlans))
            return result;

        List<PlanIndexVO> indexVOs = mapper.queryIndexOfPlans(pkPlans);
        if (CollectionUtils.isEmpty(indexVOs))
            return result;

        for (PlanIndexVO vo : indexVOs) {
            String pkPlan = vo.getPkPlan();
            if (!result.containsKey(pkPlan))
                result.put(pkPlan, new ArrayList<PlanIndexVO>());

            result.get(pkPlan).add(vo);
        }

        return result;
    }

    @Override
    public List<PlanIndexVO> queryByProjectIds(String[] pkProjects) {
        List<PlanIndexVO> planIndexVOS = mapper.queryByProjectIds(pkProjects);
        return planIndexVOS;
    }

    @Override
    public List<PlanIndexVO> queryIndexBySingleProject( String[] peasureIndexIds, String[] scheduleItems,Date startDate,Date endDate) {
        List<PlanIndexVO> indexVOs = mapper.queryIndexBySingleProject(peasureIndexIds, scheduleItems,startDate,endDate);
        if (indexVOs==null||indexVOs.size()==0){
            return null;
        }
        Map<String,PlanIndexVO> indexVOMap=new LinkedHashMap<>();
        for (PlanIndexVO planIndexVO:indexVOs){
            indexVOMap.put(planIndexVO.getPkPlanIndex(),planIndexVO);
        }
        MeasureIndexHistoryVO[] measureIndexHistoryVOS = itemQueryService.selectMeasureIndexVOByPlanIndex(indexVOMap.keySet().toArray(new String[0]));
        if (ArrayUtils.isEmpty(measureIndexHistoryVOS)){
            return indexVOs;
        }
            for (MeasureIndexHistoryVO measureIndexHistoryVO:measureIndexHistoryVOS){
                PlanIndexVO planIndexVO = indexVOMap.get(measureIndexHistoryVO.getPkPlanIndex());
                if (planIndexVO!=null){
                    planIndexVO.setCompletionsNum(measureIndexHistoryVO.getCompletionsNum());
                }
            }
        return new ArrayList<>(indexVOMap.values());
    }
}
